package com.gpframework.module.controller.common;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.common.domain.GpCommonQuestion;
import com.gpframework.module.common.domain.req.GpCommonQuestionAdd;
import com.gpframework.module.common.domain.req.GpCommonQuestionReq;
import com.gpframework.module.common.domain.req.GpCommonQuestionUpdate;
import com.gpframework.module.common.domain.vo.GpCommonQuestionVo;
import com.gpframework.module.common.service.IGpCommonQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName GpCommonQuestionController
 * @Author gpframework
 * @Date 2021-12-09
 * @Version 1.0.0
 * @Description 常见问题Controller
 */
@Validated
@Api(tags = "常见问题")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/common/gpCommonQuestion")
public class GpCommonQuestionController extends BaseController {

    private final IGpCommonQuestionService iGpCommonQuestionService;

    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonQuestion
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('common:gpCommonQuestion:list')")
    @GetMapping("/list")
    public TableDataInfo<GpCommonQuestionVo> list(GpCommonQuestionReq gpCommonQuestionReq) {
        startPage();
        QueryWrapper<GpCommonQuestion> lqw = new QueryWrapper<GpCommonQuestion>();
        lqw.orderByDesc("gp.update_time");
        gpCommonQuestionReq.generatorQuery(lqw, true);
        List<GpCommonQuestionVo> list = iGpCommonQuestionService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonQuestion
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('common:gpCommonQuestion:add')")
    @Log(title = "添加常见问题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpCommonQuestionAdd gpCommonQuestionAdd) {
        GpCommonQuestion gpCommonQuestion = Convert.convert(new TypeReference<GpCommonQuestion>() {
        }, gpCommonQuestionAdd);
        return toAjax(iGpCommonQuestionService.save(gpCommonQuestion) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonQuestion
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('common:gpCommonQuestion:edit')")
    @Log(title = "修改常见问题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpCommonQuestionUpdate gpCommonQuestionUpdate) {
        GpCommonQuestion gpCommonQuestion = Convert.convert(new TypeReference<GpCommonQuestion>() {
        }, gpCommonQuestionUpdate);
        return toAjax(iGpCommonQuestionService.updateById(gpCommonQuestion) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonQuestion
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('common:gpCommonQuestion:export')")
    @Log(title = "导出常见问题", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GpCommonQuestionReq gpCommonQuestionReq) {
        QueryWrapper<GpCommonQuestion> lqw = new QueryWrapper<GpCommonQuestion>();
        gpCommonQuestionReq.generatorQuery(lqw, true);
        List<GpCommonQuestionVo> list = iGpCommonQuestionService.listVo(lqw);
        ExcelUtil<GpCommonQuestionVo> util = new ExcelUtil<GpCommonQuestionVo>(GpCommonQuestionVo.class);
        return util.exportExcel(list, "gpCommonQuestion");
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonQuestion
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('common:gpCommonQuestion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<GpCommonQuestionVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iGpCommonQuestionService.getVoById(id));
    }


    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonQuestion
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('common:gpCommonQuestion:remove')")
    @Log(title = "删除常见问题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpCommonQuestionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
